package pers.hl.lib_global.common

import android.content.Context
import android.graphics.Bitmap
import android.text.TextUtils
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.tencent.mmkv.MMKV
import pers.hl.lib_global.R
import pers.hl.lib_global.model.bean.Book
import pers.hl.lib_global.model.enums.FileType

object GlideUtils {

    const val DEFAULT_CORNER_RADIUS = 12

    fun loadCornerImage(context: Context, view: ImageView, glideUrl: GlideUrl) {
        loadCornerImage(context, view, glideUrl, DEFAULT_CORNER_RADIUS)
    }

    fun loadCornerImage(context: Context, view: ImageView, imageUrl: String?) {
        loadCornerImage(context, view, imageUrl, DEFAULT_CORNER_RADIUS)
    }

    fun loadCornerImage(context: Context, view: ImageView, glideUrl: GlideUrl, radius: Int) {
        Glide.with(context)
            .load(glideUrl)
            .apply(RequestOptions().transforms(CenterCrop(), RoundedCorners(radius))
                                   .placeholder(R.mipmap.ic_launcher)
                                   .diskCacheStrategy(DiskCacheStrategy.NONE)
                                   .skipMemoryCache(true)
            )
            .into(view)
    }

    fun loadCornerImage(context: Context, view: ImageView, imageUrl: String?, radius: Int) {
        if (TextUtils.isEmpty(imageUrl)) {
            Glide.with(context)
                .load(R.mipmap.ic_launcher)
                .apply(
                    RequestOptions().transforms(CenterCrop(), RoundedCorners(radius))
                        .placeholder(R.mipmap.ic_launcher)
                )
                .into(view)
            return
        }
        Glide.with(context)
            .load(imageUrl)
            .apply(
                RequestOptions().transforms(CenterCrop(), RoundedCorners(radius))
                    .placeholder(R.mipmap.ic_launcher)
            )
            .into(view)
    }

    fun loadBookImage(context: Context, view: ImageView, book: Book) {
        loadCornerImage(context, view, book.bookImg)
    }

    fun loadBookQrCode(context: Context, view: ImageView, bookId: Int) {
        loadCornerImage(context, view, buildGlideUrl(FileType.value(FileType.BOOK_QR_CODE), bookId))
    }

    fun loadUserQrCode(context: Context, view: ImageView, userId: Int) {
        loadCornerImage(context, view, buildGlideUrl(FileType.value(FileType.USER_QR_CODE), userId))
    }

    fun loadUserAvatar(context: Context, view: ImageView, userId: Int, placeHolder: Int) {
        loadCornerImage(context, view, buildGlideUrl(FileType.value(FileType.USER_AVATAR), userId))
    }

    /**
     * 组装图片请求地址
     * @param type 图片类型 1:书籍照片，2:书籍二维码，3:用户头像
     * @param bussId 对应业务id
     */
    private fun buildGlideUrl(type: Int, bussId: Int): GlideUrl {
        val url = String.format(Const.IMAGE_DOWNLOAD_URL, type, bussId)
        val token = MMKV.defaultMMKV()?.decodeString(Const.KEY.KEY_TOKEN)
        return GlideUrl(
            url,
            LazyHeaders.Builder().addHeader(Const.KEY.KEY_AUTHORIZATION, token).build()
        )
    }

    fun fetchBitmap(context: Context, url: String, getBitmap: (Bitmap) -> Unit) {
        Glide.with(context).asBitmap().load(url).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                getBitmap(resource)
            }
        })
    }
}